import request from '@/utils/request'

export function getRoleDetail(id) {
  return request({
    url: '/sys/user/' + id
  })
    .then(res => res)
    .catch(() => ({}))
}

// export function getRoleList() {
//   return request({
//     url: '/sys/role'
//   })
//     .then(res => res.data.data)
//     .catch(() => [])
// }
export function getRoleList(params) {
  return request({
    url: '/sys/role',
    params
  })
    .then(res => ({
      rows: res.data.data.rows.map((d, i) => ({
        ...d,
        index: (params.page - 1) * 10 + i + 1
      })),
      total: res.data.data.total
    }))
    .catch(() => ({}))
}

export function updateRoleDetail(id, roleIds) {
  return request({
    url: '/sys/user/assignRoles',
    method: 'put',
    data: {
      id,
      roleIds
    }
  })
    .then(res => res.data.message)
    .catch(err => Promise.reject(err.response.data.message))
}
